/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package swym.protocol;

import java.io.Serializable;
import swym.server.file.SyncedFile;
import swym.server.file.FileAction;
import swym.server.file.FileChunk;

/**
 *
 * @author mschaefer
 */
public class FileActionMessage extends ActionMessage implements Serializable
{
    /*
    public static final String TYPE_NEW_FILE = "new";
    public static final String TYPE_MOD_FILE = "mod";
    public static final String TYPE_DEL_FILE = "delete";
    public static final String TYPE_DATA_FILE = "chunk"; // the payload is a file chunk
    */
    
    protected FileAction.eFileActionType fileMessageType;
    protected String path;
    protected String version;
    protected int chunkCount;
    
    /**
     * 
     * @param user
     * @param client
     * @param type
     * @param requestID
     */
    public FileActionMessage( String user, String client, FileAction.eFileActionType type, int requestID )
    {
        super( user, client, ActionMessage.TYPE_FILE, requestID );
        
        
        fileMessageType = type;
        
        path = "";
        version = "";
        chunkCount = -1;
    }
    
    public FileActionMessage( SyncedFile file, String client, FileAction.eFileActionType type, int requestID )
    {
        super( file.getOwner().getName(), client,  ActionMessage.TYPE_FILE, requestID );
        
        fileMessageType = type;
        
        setFileInformation( file );
    }
    
    /**
     * Note, that the file argument IS NOT SAVED AS A DATA MEMBER.
     * 
     * We merely take the information from the file object for 
     * use in the message;
     * 
     * @param file
     */
    public void setFileInformation( SyncedFile file )
    {
        path = file.getPath();
        version = file.getVersion();
        chunkCount = file.getSize();
    }
    
    public String getPath() { return path; }
    public String getVersion() { return version; }
    public FileAction.eFileActionType getFileActionType() { return fileMessageType; }
    public int getChunkCount() { return this.chunkCount; }
    public FileChunk getFileChunk() { return (FileChunk) getPayLoad(); }
    
    public String toString()
    {
        return super.toString() + ", " + path + ":" + version + ", " + fileMessageType;
    }
}
